package com.pierrepochon.RemoTxt.Activities;

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import com.pierrepochon.RemoTxt.R;
import com.pierrepochon.RemoTxt.Controller.MessageToSendController;
import com.pierrepochon.RemoTxt.Params.Params;
import com.pierrepochon.RemoTxt.Services.RemoTxtService;
import com.pierrepochon.RemoTxt.util.MD5Encrypter;
import com.pierrepochon.RemoTxt.util.SMSMngr;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RemoTxtActivity extends Activity {

	private TextView txtLog;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                
        Button btnRunService = (Button)findViewById(R.id.btnOK);
        Button btnAnnuler = (Button)findViewById(R.id.btnAnnuler);
        Button btnTest = (Button)findViewById(R.id.btnAnnuler);
        
        EditText txtFieldEmail = (EditText)findViewById(R.id.txtFieldEmail);
        EditText txtFieldPwd = (EditText)findViewById(R.id.txtFieldMdP);
        
        txtLog = (TextView)findViewById(R.id.log);
                
        txtFieldEmail.addTextChangedListener(new TextWatcher() {
			
        	public void afterTextChanged(Editable s) {
				Params.email = s.toString();
			}
			public void beforeTextChanged(CharSequence s, int start, int count,	int after){}
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
		});
        
        txtFieldPwd.addTextChangedListener(new TextWatcher() {
			
        	public void afterTextChanged(Editable s) {
				Params.passWord = s.toString();
			}
			public void beforeTextChanged(CharSequence s, int start, int count,	int after){}
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
		});

        btnRunService.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Params.saveAllParams(getApplicationContext());
			}
		});
        
        btnAnnuler.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				System.exit(0);
			}
		});
        
        btnTest.setOnClickListener(new OnClickListener() {
			@SuppressWarnings("deprecation")
			public void onClick(View v) {
				SmsManager.getDefault().sendTextMessage("0651523127", null, "tests", null, null);
			}
		});
        
        //chargements des params
        Params.loadAllParams(getApplicationContext());
        txtFieldEmail.setText(Params.email);
        txtFieldPwd.setText(Params.passWord);
        
        startService(new Intent(getApplicationContext(), RemoTxtService.class));
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
	  MenuInflater inflater = getMenuInflater();
	  inflater.inflate(R.layout.optionmenu, menu);
	  return true;
	}
    
    public void addLog(String txt)
    {
    	txtLog.setText(txtLog.getText() + "\n" + "[" + new Date().toString() +  "]--> " + txt);
    }
}